package com.xxgc.cloud.error.center;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xxgc.cloud.error.center.message.IBaseMsgException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @program: SpringCloudStudy
 * @description: 自定义异常父类
 * @author: Mr.abel(ShiJiaYi)
 * @create: 2022-11-23 14:39
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Component
@Slf4j
@ApiModel(value = "异常对象", description = "报错时返回")
public class BaseException extends RuntimeException implements IBaseMsgException {
    private static final long serialVersionUID = 1L;
    /**
     * 所属模块
     */
    @ApiModelProperty("模块")
    private String module;
    //公网ip
    @ApiModelProperty("公网ip")
    private String publicIpAddress;
    //私网ip
    @ApiModelProperty("私网ip")
    private List<String> privateIpAddress;
    /**
     * 错误码
     */
    @ApiModelProperty("错误码（运维人员使用）")
    private String code;
    /**
     * 错误码对应的传入参数
     */
    @ApiModelProperty("传入参数")
    private Object args;
    /**
     * 错误消息
     */
    @ApiModelProperty("错误信息")
    private String defaultMessage;
    /**
     * 接口路径 通过AOP注入
     */
    @ApiModelProperty("接口路径")
    private String path;
    /**
     * 错误的方法名
     */
    @ApiModelProperty("错误方法名")
    private String methodName;


    public BaseException(String module, String code, Object args, String defaultMessage){
        this.module = module;
        this.code = code;
        //对象转json
        //创建一个jackson对象,用来解析数据
        ObjectMapper mapper = new ObjectMapper();
        String s = null;
        try {
            s = mapper.writeValueAsString(args);
        } catch (JsonProcessingException e) {
            log.error("参数转换异常"+args.toString());
        }
        this.args = args;
        this.defaultMessage = defaultMessage;
    }

}
